<?php
/*
Plugin Name: BPDEV-Widgets
Plugin URI: http://bp-dev.org/projects/#widgets
Description: Adds widget-ready areas and widgets to BuddyPress
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com
*/

define( 'BPDEV_WIDGETS_NAME', 'BPDEV-WIDGETS' );
define( 'BPDEV_WIDGETS_VERSION', '0.3' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );
require_once( 'bpdev-widgets/bpdev-profile-sidebar.php' );
require_once( 'bpdev-widgets/bpdev-group-sidebar.php' );
require_once( 'bpdev-widgets/bpdev-widgets-profile-friends.php' );
require_once( 'bpdev-widgets/bpdev-widgets-friends-online.php' );
require_once( 'bpdev-widgets/bpdev-widgets-friends-of-you.php' );
require_once( 'bpdev-widgets/bpdev-widgets-members-wall.php' );


add_action( 'wp', 'bpdev_widgets_setup_globals', 1 );
add_action( '_admin_menu', 'bpdev_widgets_setup_globals', 1 );
	
if ( !get_site_option( 'bpdev-widgets-status' ) ) add_option( 'bpdev-widgets-status', 'off' );
if ( 'on' == get_site_option('bpdev-widgets-status') ) {

	add_action('groups_sidebar_before', 'bpdev_widget_groups_before', 2);
	add_action('groups_sidebar_after', 'bpdev_widget_groups_after', 2);

	add_action('bp_custom_profile_sidebar_boxes', 'bpdev_widget_profile', 1);
	add_action('bp_custom_profile_boxes', 'bpdev_widget_profile_boxes', 1);

	add_action( 'bpdev_theme_extra_setup_globals' , 'bpdev_widgets_css' );

}
add_action( 'bpdev_admin_plugins_screen', 'bpdev_widgets_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save', 'bpdev_widgets_admin_menu_save', 3 );

function bpdev_widgets_setup_globals() {

	global $bp;

	$bp->bpdev->widgets->slug = 'widgets';
	$bp->bpdev->widgets->image_base = site_url() . '/wp-content/mu-plugins/bpdev-widgets/images';
	$bp->bpdev->widgets->css = site_url() . '/' . bpdev_component_dir( $bp->bpdev->widgets->slug ) . 'css/';

}

function bpdev_widgets_admin_menu_save() {

	do_action( 'bpdev_admin_save_component', 'bpdev-widgets-status', false );
	do_action( 'bpdev_widgets_extra_admin_save' );

}

function bpdev_widgets_admin_menu_screen() {

	?>

	<h3><?php echo BPDEV_WIDGETS_NAME; ?></h3>

	<table class="form-table">

		<tr valign="top">

			<th scope="row">Plugin Version</th>
			<td>
				<?php echo BPDEV_WIDGETS_VERSION; ?>
			</td>

		</tr>

		<tr>

			<th scope="row">Plugin Status</th>
			<td>

				<select id="bpdev-widgets-status" name="bpdev-widgets-status">

					<option value="off"  <?php if ( 'off' == get_site_option( 'bpdev-widgets-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( 'on' == get_site_option('bpdev-widgets-status') ) echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>

<?php do_action( 'bpdev_widgets_extra_admin_screen' ); ?>

	</table>

	<?php
}

function bpdev_widgets_css() {

	bpdev_theme_register_style(
		'bpdev-widgets', 
		'BPDEV Widgets', 
		array( 'BPDEV_Theme_CSS' , 'bpdev_widgets' ),
		'on',
		'off'
	);

}

/* Sidebar Hooks */
function bpdev_widget_profile() {

	do_action('bpdev_widget_profile');

}

function bpdev_widget_profile_boxes() {

	do_action('bpdev_widget_profile_boxes');

}

function bpdev_widget_groups_before() {

	do_action('bpdev_widget_groups_before');

}

function bpdev_widget_groups_after() {

	do_action('bpdev_widget_groups_after');

}
?>